package com.app.controller.miniprogram;

import com.app.common.Result;
import com.app.model.UserModel;
import com.app.service.UserService;
import com.app.util.TokenUtils;
import com.app.vo.UserLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;


@Controller
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public Result<UserLoginVo> login(@RequestParam(value = "code") String code,
                                     @RequestParam(value = "mobileCode") String mobileCode, HttpServletRequest request) throws LoginException {
        //微信登录
        UserModel user = userService.wxLogin(code, mobileCode, request);
        //生成token
        String token = TokenUtils.sign(user.getOpenid());
        UserLoginVo userLoginVO = UserLoginVo.builder()
                .user(user)
                .token(token)
                .build();
        System.out.println(token);
        return Result.success(userLoginVO);
    }

    //编辑用户信息
    @GetMapping("/updateUserInfo")
    public Result<Object> updateUserInfo(String userName, String mobile, String faceUrl, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openid = TokenUtils.nothingness(token);
        if (openid==null){
            return Result.error(405, "token过期");
        }
        UserModel user = userService.getUserByOpenid(openid);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        if (mobile != null) {
            user.setMobile(mobile);
        }
        if (userName != null) {
            user.setUserName(userName);
        }
        if (faceUrl != null) {
            user.setFaceUrl(faceUrl);
        }
        userService.updateUserInfo(user);
        return Result.success("操作成功");
    }


    //修改头像
    @GetMapping("/updateAvatar")
    public Result<Object> updateAvatar(String avatar, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String mobile = TokenUtils.nothingness(token);
        if (mobile==null){
            return Result.error(405, "token过期");
        }
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        user.setFaceUrl(avatar);
        UserModel userModel = userService.updateAvatar(user);
        return Result.success(userModel);
    }

    //查询个人信息
    @GetMapping("/selectUserById")
    public Result<Object> selectUserById(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String mobile = TokenUtils.nothingness(token);
        if (mobile==null){
            return Result.error(405, "token过期");
        }
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(userService.selectUserById(user.getId()));
    }

}
